package com.graphics;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/
import java.awt.Color;
import java.awt.Graphics;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.math.Quat;
import com.math.Vector;
public class Box extends Shape{
public Box(Camera cam,Vector dim,Vector pos,Quat rot,Color color){//Vector rot
super();
this.cam = cam;
this.def_color = color;
d = new Vector(dim);
points_num = 8;
points = new Vector[points_num];
this.p = new Vector(pos);
// this.r = new Vector(rot);
this.r = new Quat(rot);
guf = new Vector[6][4];
x = new int[4];
y = new int[4];
build();
rebuild();
}
public Box(Vector dim,Vector pos,Quat rot,Color color){//Vector rot
super();
this.def_color = color;
d = new Vector(dim);
points_num = 8;
points = new Vector[points_num];
this.p = new Vector(pos);
// this.r = new Vector(rot);
this.r = new Quat(rot);
guf = new Vector[6][4];
x = new int[4];
y = new int[4];
build();
}
public void build(){
for(int i = 0;i<points_num;i++)
points[i] = new Vector();
bridg();
}
public void bridg(){
//up
guf[0][0] = points[0];guf[0][1] = points[1];guf[0][2] = points[2];guf[0][3] = points[3];
//right
guf[1][0] = points[1];guf[1][1] = points[2];guf[1][2] = points[6];guf[1][3] = points[5];
//down
guf[2][0] = points[4];guf[2][1] = points[7];guf[2][2] = points[6];guf[2][3] = points[5];
//left
guf[3][0] = points[0];guf[3][1] = points[3];guf[3][2] = points[7];guf[3][3] = points[4];
//out
guf[4][0] = points[0];guf[4][1] = points[1];guf[4][2] = points[5];guf[4][3] = points[4];
//in
guf[5][0] = points[3];guf[5][1] = points[2];guf[5][2] = points[6];guf[5][3] = points[7];
}
public void rebuild(){
points[0].set(-d.x,+d.y,-d.z);
points[1].set(+d.x,+d.y,-d.z);
points[2].set(+d.x,+d.y,+d.z);
points[3].set(-d.x,+d.y,+d.z);
points[4].set(-d.x,-d.y,-d.z);
points[5].set(+d.x,-d.y,-d.z);
points[6].set(+d.x,-d.y,+d.z);
points[7].set(-d.x,-d.y,+d.z);
for(int i=0;i<8;i++){
// Vector.transformation(r,p,points[i]);
// Vector.reverseTransformation(cam.r, cam.p, points[i]);
points[i].transformation(r,p);
points[i].reverseTransformation(cam.r, cam.p);
}
tmp.reverseTransformation(cam.r, cam.p,p);
compare_z = (int)(tmp.z);
}
public void show(Graphics g){
calcColor();
if(!TRANSPARENT){
int tmp = findClosestPoint();
for(int i = 0;i<6;i++){
if((guf[i][0].z > 0)&&(guf[i][1].z > 0)&&(guf[i][2].z > 0)&&(guf[i][3].z > 0)){
if(guf[i][0]==points[tmp]||guf[i][1]==points[tmp]||guf[i][2]==points[tmp]||guf[i][3]==points[tmp]){
this.convert(guf[i],x,y);
g.setColor(color);
if(this.only_lines)
g.drawPolygon(x, y, 4);
else
g.fillPolygon(x, y, 4);
}
}
}
}else{//for transparent objects
for(int i = 0;i<6;i++){
if((guf[i][0].z > 0)&&(guf[i][1].z > 0)&&(guf[i][2].z > 0)&&(guf[i][3].z > 0)){
this.convert(guf[i],x,y);
g.setColor(color);
if(this.only_lines)
g.drawPolygon(x, y, 4);
else
g.fillPolygon(x, y, 4);
}
}
}
}
public Color getColor(int i){
float t = 10;
float f = ((i)+t)/t;
int rr = (int) (color.getRed()*f);
int gg = (int) (color.getGreen()*f);
int bb = (int) (color.getBlue()*f);
if(rr > 255)
rr = 255;
if(gg > 255)
gg = 255;
if(bb > 255)
bb = 255;
return new Color(rr,gg,bb);
}
public Element toXML(Document doc){
Element box = doc.createElement("Box");
box.setAttribute("position", p+"");
box.setAttribute("rotation", r+"");
box.setAttribute("dimmention", d+"");
box.setAttribute("color", def_color.getRGB()+"");
return box;
}
public static Box fromXML(Element e, Camera cam){
Color color = new Color(Integer.parseInt(e.getAttribute("color")));
Vector d = Vector.parseVector(e.getAttribute("dimmention"));
Vector p = Vector.parseVector(e.getAttribute("position"));
Quat r = Quat.parseQuat(e.getAttribute("rotation"));
return new Box(cam,d,p,r,color);
}
}